//
//  XMN_OpinionViewController.m
//  B段项目
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "XMN_OpinionViewController.h"
#import <MessageUI/MFMailComposeViewController.h>

@interface XMN_OpinionViewController ()<UITextViewDelegate, MFMailComposeViewControllerDelegate>
@property(nonatomic, strong)UITextView *textField;
@end

@implementation XMN_OpinionViewController

-(void)loadView{
    [super loadView];
    
    //创建控件
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(25 * FitWidth, 30 * FitHeight, ScreenWidth - 40 * FitWidth, 30 * FitHeight)];
    label.text = @"感谢您的宝贵意见,让我们更完善:";
    label.font = [UIFont systemFontOfSize:17];
    label.textColor = [UIColor lightGrayColor];
    [self.view addSubview:label];
    
    _textField = [[UITextView alloc]initWithFrame:CGRectMake(20 * FitWidth, 60 * FitHeight, ScreenWidth - 40 * FitWidth, 240 * FitHeight)];
    _textField.delegate = self;
    _textField.layer.borderWidth = 1.0;
    _textField.layer.borderColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1].CGColor;
    _textField.layer.cornerRadius = 7;
    [_textField becomeFirstResponder];
    _textField.font = [UIFont systemFontOfSize:15];
    _textField.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1];
    [self.view addSubview:_textField];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(ScreenWidth - 70 * FitWidth, _textField.frame.origin.y + _textField.frame.size.height + 10 * FitHeight, 50 * FitWidth, 25 * FitHeight);
    button.layer.cornerRadius = 5;
    button.layer.borderWidth = 1.0;
    button.layer.borderColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1].CGColor;
    [button setTitle:@"发送" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:12];
    [button addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

#pragma mark - 回收键盘
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_textField resignFirstResponder];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - button触发事件
-(void)clickAction{
    [_textField resignFirstResponder];
    
    if ([_textField.text isEqualToString:@""]) {
        DXAlertView *clert = [[DXAlertView alloc]initWithTitle:@"反馈信息不能为空" contentText:@"请重新输入" leftButtonTitle:nil rightButtonTitle:@"确定"];
        [clert show];
    }else{
        Class mailClass = NSClassFromString(@"MFMailComposeViewController");
        if (mailClass != nil) {
            if ([mailClass canSendMail]) {
                [self displayComposerSheet];
            }else{
                [self launchMailAppOnDevice];
            }
        }else{
            [self launchMailAppOnDevice];
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"意见反馈";
}


-(void)displayComposerSheet
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc]init];
    picker.mailComposeDelegate = self;
    [picker setSubject:@"邮件主题"];
    NSArray *toRecipient = [NSArray arrayWithObject:@"GZ906571118@163.com"];
    [picker setToRecipients:toRecipient];
    NSString *emailBody = _textField.text;
    [picker setMessageBody:emailBody isHTML:NO];
    [self presentViewController:picker animated:YES completion:nil];
}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    [self dismissViewControllerAnimated:YES completion:nil];
    _textField.hidden = NO;
    switch (result) {
        case MFMailComposeResultSent:{
            DXAlertView *alert = [[DXAlertView alloc]initWithTitle:@"发送成功" contentText:@"是否留在反馈页面" leftButtonTitle:@"返回" rightButtonTitle:@"停留"];
            [alert show];
            alert.leftBlock = ^(){
                [self.navigationController popViewControllerAnimated:YES];
            };
            alert.rightBlock = ^(){
                _textField.text = @"";
            };
            break;
        }
        case MFMailComposeResultFailed:{
            DXAlertView *alert = [[DXAlertView alloc]initWithTitle:@"发送失败" contentText:@"是否重新发送" leftButtonTitle:@"取消" rightButtonTitle:@"重新发送"];
            [alert show];
            alert.rightBlock = ^(){
                [self clickAction];
            };
            break;
        }
        case MFMailComposeResultCancelled:{
            DXAlertView *alert = [[DXAlertView alloc]initWithTitle:@"提示:" contentText:@"已取消发送" leftButtonTitle:nil rightButtonTitle:@"知道了"];
            [alert show];
            break;
        }
        case MFMailComposeResultSaved:{
            DXAlertView *alert = [[DXAlertView alloc]initWithTitle:@"提示:" contentText:@"已保存" leftButtonTitle:nil rightButtonTitle:@"确定"];
            [alert show];
            break;
        }
        default:
            break;
    }
    if (error) {
        DXAlertView *alert = [[DXAlertView alloc]initWithTitle:@"反馈信息出错" contentText:error.localizedDescription leftButtonTitle:nil rightButtonTitle:@"知道了"];
        [alert show];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
-(void)launchMailAppOnDevice
{
    NSString *recipients = @"mailto:GZ906571118@163.com&subject=用户反馈!";
    NSString *body = [NSString stringWithFormat:@"&body=%@",_textField.text];
    NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
    email = [email stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:email]];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
